import 'package:flutter/material.dart';
import 'home.dart';
import 'category.dart';
import 'cart.dart';
import 'my.dart';
import 'package:dio/dio.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        bottomAppBarTheme: BottomAppBarTheme(color: Colors.blue),
      ),
      home: MyHomePage(title: '首页'),
    );
  }
}

///主页面page
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  var pageList;
  var tabImages;
  Image getImageAssert(path){
    return Image.asset(path,width: 24,height: 24,);
  }
  ///登录
  void login() async {
    Response response;
    Dio dio = new Dio();
// Optionally the request above could also be done as
    response = await dio.get("http://172.19.135.64:9000/reqnewVersion", queryParameters: {"version": 1});
//    response = await dio.post("http://172.19.135.64:9000/login", data: {"openid": "oPMYr0bctsGqKi5Z6XfTTOi63r8U", "token": "wendu"});
    print(response.data.toString());
  }
  ///初始化操作
  void init(){
    login();
    tabImages =[
      [getImageAssert('images/home/icon_home.png'),getImageAssert('images/home/icon_home_pressed.png')],
      [getImageAssert('images/home/icon_category.png'),getImageAssert('images/home/icon_category_pressed.png')],
      [getImageAssert('images/home/icon_cart.png'),getImageAssert('images/home/icon_cart_pressed.png')],
      [getImageAssert('images/home/icon_my.png'),getImageAssert('images/home/icon_my_pressed.png')]
    ];
    pageList =[
      HomePage(),
      Category(),
      CartPage(),
      MyPage()
    ];
  }
  Image getTabIcon(int index){
      if(_currentIndex == index){
        return tabImages[index][1];
      }else {
        return tabImages[index][0];
      }
  }
  Text getTabText(name){
    return Text(name,style: TextStyle(color: Colors.blue,fontSize: 12));
  }
  @override
  Widget build(BuildContext context) {
    init();
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(items:
      <BottomNavigationBarItem>[
        BottomNavigationBarItem(icon: getTabIcon(0),title: getTabText('首页'),backgroundColor: Colors.white),
        BottomNavigationBarItem(icon: getTabIcon(1),title: getTabText('分类'),backgroundColor: Colors.white),
        BottomNavigationBarItem(icon: getTabIcon(2),title: getTabText('购物车'),backgroundColor: Colors.white),
        BottomNavigationBarItem(icon: getTabIcon(3),title: getTabText('我的'),backgroundColor: Colors.white)],
        type: BottomNavigationBarType.shifting,
        currentIndex: _currentIndex,
        iconSize: 24,
        onTap: (index){
          setState(() {
            _currentIndex=index;
          });
        },
      ),
      body: pageList[_currentIndex],
    );
  }
}
